package Aleehot100;

import C017.main;

/**
 * ClassName: c023
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/6/12 08:52
 * @Version 1.0
 * @Since 1.0
 */
public class c023 {
    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        ListNode listNode = reverseList(listNode1);

        while(listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }

    static class ListNode {
        int val;
        ListNode next;
        public ListNode(int val) {
            this.val = val;
            next = null;
        }
    }
    public static ListNode reverseList(ListNode root) {
        if(root == null || root.next == null) {
            return root;
        }

        ListNode pre = null;
        ListNode cur = root, mid;
        do {
            mid = cur.next;
            cur.next = pre;
            pre = cur;
            cur = mid;
        }while(cur != null);

        return pre;
    }
}
